//
//  PlaceholderView.m
//  YMPalceholder
//
//  Created by lantaiyuan on 16/12/28.
//  Copyright © 2016年 youmy. All rights reserved.
//

#import "PlaceholderView.h"

@interface PlaceholderView ()
@property (nonatomic, strong) UIButton *reloadButton;
@property (nonatomic, assign) CGFloat y;
@property (nonatomic, assign) BOOL show;
@end

@implementation PlaceholderView

- (instancetype)initWithFrame:(CGRect)frame orginy:(CGFloat)y{
    if (self = [super initWithFrame:frame]) {
        self.y = y;
        [self layoutSubviews];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame orginy:(CGFloat)y reloadBtn:(BOOL)show{
    if (self = [super initWithFrame:frame]) {
        self.y = y;
        self.show = show;
        [self layoutSubviews];
    }
    return self;
}


- (void)layoutSubviews {
    [super layoutSubviews];
    [self createUI];
}

- (void)createUI {
    self.backgroundColor = CONTENT_BG_COLOR;
    
    [self addSubview:self.imageView];
    [self addSubview:self.titleLabel];
    [self addSubview:self.reloadButton];
    
    self.reloadButton.hidden = !self.show;
    
    WEAKSELF
    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(weakSelf.mas_centerX);
        make.top.equalTo(@60);
        make.width.equalTo(@132);
        make.height.equalTo(@114);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.imageView.mas_bottom).offset(20);
        make.centerX.equalTo(weakSelf.imageView.mas_centerX);
        make.width.mas_equalTo(weakSelf.mas_width).offset(30);
        make.height.equalTo(@21);
    }];
    
    [self.reloadButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.titleLabel.mas_bottom).offset(65);
        make.centerX.equalTo(weakSelf.titleLabel.mas_centerX);
        make.width.equalTo(@210);
        make.height.equalTo(@40);
    }];
}

- (UIButton*)reloadButton {
    if (!_reloadButton) {
        _reloadButton = [UIButton buttonWithType:UIButtonTypeCustom];
//        _reloadButton.backgroundColor = [UIColor redColor];
        _reloadButton.titleLabel.font = [UIFont systemFontOfSize:17];
        [_reloadButton setTitle:@"去接单" forState:UIControlStateNormal];
        [_reloadButton setTitleColor:[UIColor colorWithHexString:@"#2ab650"] forState:UIControlStateNormal];
        WEAKSELF
        [[_reloadButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            if(weakSelf.reloadClickBlock){
                weakSelf.reloadClickBlock();
            }
        }];
        [_reloadButton drawBorderWithColor:[UIColor colorWithHexString:@"#2ab650"] radius:5];
    }
    return _reloadButton;
}

- (UIImageView *)imageView{
    if (!_imageView) {
        _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
//        _imageView.image = [UIImage imageNamed:@"nocollection"];
    }
    return _imageView;
}

- (UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.textColor = SUB_TITLE_COLOR;
        _titleLabel.font = [UIFont systemFontOfSize:18];
//        _titleLabel.text = Localized(@"No record collection") ;
        _titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLabel;
}

- (void)reloadClick:(UIButton*)button {
    if (self.reloadClickBlock) {
        self.reloadClickBlock();
    }
}

- (void)setImage:(NSString *)imageName title:(NSString *)title{
    [self.imageView setValue:[UIImage imageNamed:imageName] forKey:@"image"];
    [self.titleLabel setValue:title forKey:@"text"];
}

@end
